﻿using System;
using QxFramework.Core;
using UnityEngine;

public class PlayerAnimatorControler : MonoBehaviour
{
    private float recoverTime;
    private PlayerAttackCheck attackCheck;
    private PlayerControl player;
    private string currentAttackType;

    public float maxRecoverTime;
    public bool attacking;

    public void Awake()
    {
        player = transform.parent.GetComponent<PlayerControl>();
        attackCheck = transform.parent.Find("AttackCheck").GetComponent<PlayerAttackCheck>();
    }

    public void Update()
    {
        if (recoverTime > 0)
        {
            recoverTime -= Time.unscaledDeltaTime;
        }
        else
        {
            StartAnimation();
        }
    }

    public void FixedUpdate()
    {
        if (currentAttackType != null && attacking)
        {
            Attack(currentAttackType);
        }
    }

    private void SlowAnimation()
    {
        //Debug.Log("播放减缓");
        Time.timeScale = 0;
        recoverTime = maxRecoverTime;
    }
    private void StartAnimation()
    {
        Time.timeScale = 1;
    }
    /*
    
    public void FixedUpdate()
    {
        if (recoverTime > 0)
        {
            recoverTime -= Time.fixedDeltaTime / Time.timeScale;
        }
        else
        {
            StartAnimation();
        }

        if (currentAttackType != null)
        {
            Attack(currentAttackType);
        }
    }

    private void SlowAnimation()
    {
        //Debug.Log("播放减缓");
        GameMgr.Get<ITimeManager>().SetTimeScale(0.1f);
        recoverTime = maxRecoverTime;
    }
    private void StartAnimation()
    {
        GameMgr.Get<ITimeManager>().SetTimeScale(1);
    }

    */

    /// <summary>
    /// 攻击判定产生，将会伤害检测到的敌人
    /// </summary>
    private void Attack(string attackType)
    {
        bool ifhit = false;
        if (attackCheck.UseAttack(attackType) != null)
        {
            PlayerAttack attack = attackCheck.UseAttack(attackType);
            if (attack.DetectedList.Count > 0)
            {
                foreach (var enemy1 in attack.DetectedList)
                {
                    if (!enemy1.isDead)
                    {
                        bool isSame = false;
                        foreach (var enemy2 in attack.AttackedList)
                        {
                            if (enemy1 == enemy2)
                            {
                                isSame = true;
                            }
                        }
                        if (!isSame)
                        {
                            //Debug.Log("击中");
                            GameMgr.Get<IEnemyManager>().Damage(enemy1, player.playerAttackCheck.UseAttack(player.playerAttackCheck.GetCurrentAttack()).damage);
                            attack.AttackedList.Add(enemy1);
                            ifhit = true;
                        }
                    }
                }
            }
            if (ifhit)
            {
                GameMgr.Get<ICameraManager>().StartShake(0.166666f, 0.1f);
                //AudioManager.Instance.PlayEffectAudio("NormalHit", 0.15f, false);
                SlowAnimation();
            }
        }
    }

    public void BeforeAttack()
    {
        MessageManager.Instance.Get<AttackPeriod>().DispatchMessage(AttackPeriod.BeforeAttack, player);
    }

    public void StartAttack(string attackType)
    {
        attacking = true;
        currentAttackType = attackType;
        MessageManager.Instance.Get<AttackPeriod>().DispatchMessage(AttackPeriod.StartAttack, player);
    }

    public void EndAttack()
    {
        attacking = false;
        attackCheck.UseAttack(currentAttackType).AttackedList.Clear();
        currentAttackType = null;
        MessageManager.Instance.Get<AttackPeriod>().DispatchMessage(AttackPeriod.EndAttack, player);
    }

    public void AfterAttack()
    {
        MessageManager.Instance.Get<AttackPeriod>().DispatchMessage(AttackPeriod.AfterAttack, player);
    }

    public void BreakAttack()
    {
        attacking = false;
        attackCheck.UseAttack(currentAttackType).AttackedList.Clear();
        currentAttackType = null;
    }

    public enum AttackPeriod
    {
        BeforeAttack,
        StartAttack,
        EndAttack,
        AfterAttack,
    }
}